package com.yuyou.fn.portal.service.impl;

import com.yuyou.fn.common.service.ISiteConfigService;
import com.yuyou.fn.common.service.IWxTemplateMessage;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.platform.entity.Oauth;
import com.yuyou.fn.portal.entity.OuterOrder;
import com.yuyou.fn.portal.service.IPushMessageService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PushMessageServiceImpl implements IPushMessageService{


    @Resource
    private IWxTemplateMessage wxTemplateMessage ;

    @Resource
    private ISiteConfigService siteConfigService ;


    @Override
    public void pushPaysSuccessMessageToWX(Long outerOrderId, BigDecimal paidAmount, OuterOrder outerOrder, Oauth oauth) {
        List<Map<String,Object>> params = new ArrayList<>(3);
        Map<String,Object> first = new HashMap<>();
        first.put("color","#000000");
        first.put("name","first" );
        first.put("value","订单缴费成功");
        Map<String,Object> keyword1 = new HashMap<>();
        keyword1.put("color","#000000");
        keyword1.put("name","keyword1" );
        keyword1.put("value",outerOrder.getOuterOrderId());
        Map<String,Object> keyword2 = new HashMap<>();
        keyword2.put("color","#000000");
        keyword2.put("name","keyword2" );
        keyword2.put("value",paidAmount);
        Map<String,Object> keyword3 = new HashMap<>();
        keyword3.put("color","#000000");
        keyword3.put("name","keyword3" );
        keyword3.put("value", Utils.formatDate(outerOrder.getUpdateTime(),"yyyy-MM-dd") );

        Map<String,Object> remark = new HashMap<>();
        remark.put("color","#000000");
        remark.put("name","remark" );
        remark.put("value","点击查看订单");
        params.add(first);
        params.add(keyword1);
        params.add(keyword2);
        params.add(keyword3);
        params.add(remark);

        String mpAppOrderDetailUrl = siteConfigService.getConfigValueFromCache("mp_app_order_detail_url");
        wxTemplateMessage.send("t9Xa7R0vra-IHSBudlCz29rYcEgXIthN9T_MH4MA2kw",oauth.getOpenId(),mpAppOrderDetailUrl.replace("#orderId#",String.valueOf(outerOrderId) ) , params) ;
    }
}
